<?php
class ModelCatalogReview extends Model {
	public function addReview($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "review SET author = '" . $this->db->escape($data['author']) . "', product_id = '" . (int)$data['product_id'] . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', rating = '" . (int)$data['rating'] . "', status = '" . (int)$data['status'] . "', date_added = '" . $this->db->escape($data['date_added']) . "'");

		$review_id = $this->db->getLastId();

		$this->cache->delete('product');

		return $review_id;
	}

	public function editReview($id, $data) {
	    $this->db->query("update " . DB_PREFIX . "product_comments set comments = '" . $this->db->escape($data['comments']) . "', rating = '" . (int)$data['rating'] . "', date_modified=NOW(), status=" . (int)$data['status'] . " where id=" . (int)$id);

	    $this->db->query("delete from " . DB_PREFIX . "product_comments_image where comments_id=" . (int)$id);
	    if (isset($data['images']) && $data['images']) {
	        foreach ($data['images'] as $image) {
                $this->db->query("insert into " . DB_PREFIX . "product_comments_image set comments_id=" . (int)$id . ", image='" . $this->db->escape($image) . "'");
            }
        }
		$this->cache->delete('product');
	}

	public function deleteReview($id) {

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_comments WHERE id = '" . (int)$id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_comments_image WHERE comments_ids = '" . (int)$id . "'");

		$this->cache->delete('product');
	}

	public function getReview($review_id) {
		$review_main = $this->db->query("select * from " . DB_PREFIX . "product_comments where id=" . (int)$review_id)->row;
		if ($review_main) {
		    $comment_images = array();
		    $images = $this->db->query("select * from " . DB_PREFIX . "product_comments_image where comments_id=" . (int)$review_id)->rows;
		    if ($images) {
		        foreach ($images as $image) {
		            $comment_images[] = $image['image'];
                }
            }
            $review_main['images'] = $comment_images;
        }
		return $review_main;
	}

	public function getReviews($data = array()) {
	    /*
		$sql = "SELECT r.review_id, pd.name, r.author, r.rating, r.status, r.date_added FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product_description pd ON (r.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

		if (!empty($data['filter_product'])) {
			$sql .= " AND pd.name LIKE '" . $this->db->escape($data['filter_product']) . "%'";
		}

		if (!empty($data['filter_author'])) {
			$sql .= " AND r.author LIKE '" . $this->db->escape($data['filter_author']) . "%'";
		}

		if (isset($data['filter_status']) && $data['filter_status'] !== '') {
			$sql .= " AND r.status = '" . (int)$data['filter_status'] . "'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(r.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}
	    */
	    $sql = "select * from " . DB_PREFIX . "product_comments where 1";

        if (!empty($data['filter_product'])) {
            $sql .= " AND binary product_name LIKE '%" . $this->db->escape($data['filter_product']) . "%'";
        }

        if (!empty($data['filter_author'])) {
            $sql .= " AND customer_name LIKE '%" . $this->db->escape($data['filter_author']) . "%'";
        }

        if (isset($data['filter_status']) && $data['filter_status'] !== '') {
            $sql .= " AND status = '" . (int)$data['filter_status'] . "'";
        }

        if (!empty($data['filter_date_added'])) {
            $sql .= " AND DATE(date_created) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
        }

		$sort_data = array(
			'product_name',
			'customer_name',
			'rating',
			'status',
			'date_created'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY CONVERT(" . $data['sort'] . " USING gbk)";
		} else {
			$sql .= " ORDER BY date_created";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$rows = $this->db->query($sql)->rows;
		return $rows;
	}

	public function getTotalReviews($data = array()) {
		//$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r LEFT JOIN " . DB_PREFIX . "product_description pd ON (r.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
        $sql = "select count(*) total from " . DB_PREFIX . "product_comments where 1";
		if (!empty($data['filter_product'])) {
			$sql .= " AND binary product_name LIKE '%" . $this->db->escape($data['filter_product']) . "%'";
		}

		if (!empty($data['filter_author'])) {
			$sql .= " AND customer_name LIKE '%" . $this->db->escape($data['filter_author']) . "%'";
		}

		if (isset($data['filter_status']) && $data['filter_status'] !== '') {
			$sql .= " AND status = '" . (int)$data['filter_status'] . "'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(date_created) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}
		$query = $this->db->query($sql);

		return $query->row['total'];
	}

	public function getTotalReviewsAwaitingApproval() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review WHERE status = '0'");

		return $query->row['total'];
	}
}